<?php
namespace Kuoqi\DbDriver\Sql;
class Table{
	public $name = '';
	public $alias = '';
	
	//简单的输入对象
	public function __construct($tbname,$alias){
		$tbname = str_replace('***','',$tbname);
		$this->name = $this->tableMiddleware(str_replace('\s','',$tbname));
		$this->alias = str_replace('\s','',$alias);
	}
	
	final function tableMiddleware($str){
		$opt = '';
		for($i=0;$i<strlen($str);$i++){
			if((($ascii = ord($str[$i]))>=65)&&($ascii<=90)){
				$opt .= $opt?('_'.chr($ascii+32)):chr($ascii+32);
			}else{
				$opt .= $str[$i];	
			}
		}
		return $opt;
	}
	
	//单纯的输出，格式化
	public function output(){
		return sprintf('***%s%s',
			$this->name,
			$this->alias?(" AS ".$this->alias):''
		);
	}
	
}